package com.shijun.pullrecyclerviewdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shijun.adapter.MyAdapter;
import com.shijun.adapter.RecyclerWrapAdapter;
import com.shijun.bean.DataBean;
import com.shijun.customview.CustomLinearLayoutManager;
import com.shijun.customview.CustomRecyclerView;
import com.shijun.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements CustomRecyclerView.OnScrollActionListener, CustomRecyclerView.OnScrollTopOrBottomListener ,MyAdapter.OnListener{

    private static final int ACTION_RECOVER = 201;
    private CustomRecyclerView customRecyclerView;
    private MyAdapter adapter;
    private View headView;
    private boolean isOnTop;//是否滑到顶端
    private boolean isOnBottom;//是否滑到底端
    private int headViewHeight;//headView即时的高度
    private int headViewInitHeight;//headView初始的高度
    private int headViewMaxHeight;//headView最大的高度
    private float old_Y;//滑动某个点时记录的上次记录点的值
    private float new_Y;//滑动某个点时记录的值
    private float increase;//滑动值
    private boolean isOverMax;//高度超过最大限度
    private boolean isRecover;//是否在恢复状态
    private RelativeLayout headViewLayout;
    private CustomLinearLayoutManager linearLayoutManager;
    private List<DataBean> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        customRecyclerView = (CustomRecyclerView) findViewById(R.id.activity_main_content_crv);
        linearLayoutManager = new CustomLinearLayoutManager(this);
        customRecyclerView.setLayoutManager(linearLayoutManager);
        customRecyclerView.setOnScrollActionListener(this);
        customRecyclerView.setOnScrollTopOrBottomListener(this);

        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth() - getResources().getDimensionPixelSize(R.dimen.x160);

        dataList = new ArrayList<>();
        dataList.addAll(Utils.getData());
        adapter = new MyAdapter(this, dataList, width);
        adapter.setOnListener(this);
        headView = LayoutInflater.from(this).inflate(R.layout.inflater_main_content_head, null, false);
        headViewLayout = (RelativeLayout) headView.findViewById(R.id.inflater_main_content_head_rl);

        headViewLayout.post(new Runnable() {
            @Override
            public void run() {
                headViewHeight = headViewLayout.getHeight();
                headViewInitHeight = headViewHeight;
                headViewMaxHeight = (int) (headViewHeight * 8);
            }
        });


        List<View> headViews = new ArrayList<>();
        headViews.add(headView);
        customRecyclerView.setAdapter(new RecyclerWrapAdapter(headViews, null, adapter));
    }

    @Override
    public void onScrollDown(float startX, float startY) {
        new_Y = startY;
    }

    @Override
    public void onScrollMove(float startX, float startY, float currentX, float currentY) {
        if (isOnTop && startY - currentY < -50 && !isRecover) {
            old_Y = new_Y;
            new_Y = currentY;
            increase = increase + (new_Y - old_Y);
            linearLayoutManager.setScrollEnabled(false);
                    headViewHeight = (int) (headViewInitHeight + increase /3);
            if (!isOverMax) {
                if (headViewHeight < headViewMaxHeight && headViewHeight > headViewInitHeight) {
                    refreshHeadView(headViewHeight);
                } else if (headViewHeight >= headViewMaxHeight) {
                    isOverMax = true;
                }
            } else {
                doRecover();
            }
        }
    }

    @Override
    public void onScrollUp(float startX, float startY) {
        if (isOnTop && headViewHeight > headViewInitHeight && !isRecover && !isOverMax) {
            doRecover();
        }
        Log.i("TAG","isOnBottom:"+isOnBottom);
    }

    @Override
    public void onScrollTop() {
        isOnTop = true;
    }

    @Override
    public void onScrollBottom() {
        isOnBottom = true;
    }

    @Override
    public void onScrollMiddle() {
        isOnTop = false;
        isOnBottom = false;
    }

    private void refreshHeadView(float height) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) headViewLayout.getLayoutParams();
        params.height = (int) height;
        headViewLayout.setLayoutParams(params);
        headViewLayout.invalidate();
    }

    private void doRecover() {
        isRecover = true;
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int startHeight = (int) headViewHeight;
            int interval = 5;

            @Override
            public void run() {
                Message msg = new Message();
                msg.what = ACTION_RECOVER;
                if (startHeight > headViewInitHeight) {
                    msg.arg1 = startHeight;
                    handle.sendMessage(msg);
                    startHeight = startHeight - interval;
                    interval += 2;
                } else if (startHeight == headViewInitHeight) {
                    timer.cancel();
                    increase = 0;
                    isOverMax = false;
                    isRecover = false;
                    headViewHeight = headViewInitHeight;
                    linearLayoutManager.setScrollEnabled(true);
                } else {
                    msg.arg1 = headViewInitHeight;
                    handle.sendMessage(msg);
                    startHeight = headViewInitHeight;
                }
            }
        }, 0, 10);
    }

    private Handler handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case ACTION_RECOVER:
                    refreshHeadView(msg.arg1);
                    break;
            }
        }
    };

    public void doRequestLike(int postion,MyAdapter.MyViewHolder holder){
        //----------------------------------------------------

        //----------------------------------------------------
        dataList.get(postion).getLiskes().add(new DataBean.UserBean("我",100));
        adapter.refreshCustomAutoLineFeedView(postion,holder);

    }

    public void doRequestUnLike(int postion,MyAdapter.MyViewHolder holder){
        //----------------------------------------------------

        //----------------------------------------------------
        for(DataBean.UserBean user:dataList.get(postion).getLiskes()) {
            if(user.getUserId()==100){
                dataList.get(postion).getLiskes().remove(user);
            }
        }
        adapter.refreshCustomAutoLineFeedView(postion,holder);

    }

    @Override
    public void onCustomAutoLineFeedViewItemClick(TextView view, int postion,MyAdapter.MyViewHolder holder) {
        Toast.makeText(this, "位置："+postion+" 名字："+view.getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCustomListShowViewItemClick(TextView view, int postion) {
        Toast.makeText(this, "位置："+postion+" 名字："+view.getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLikeClick(boolean isLike, int postion,MyAdapter.MyViewHolder holder) {
        if(isLike){
            doRequestLike(postion,holder);
        }else{
            doRequestUnLike(postion,holder);
        }
    }
}
